package com.org.manhu.chargehelper.ui;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.org.manhu.chargehelper.ChargeApplication;
import com.org.manhu.chargehelper.R;
import com.org.manhu.chargehelper.broadcastreceiver.UpdateMoneyReceiver;
import com.org.manhu.chargehelper.broadcastreceiver.UpdateNickNameReceiver;
import com.org.manhu.chargehelper.constant.Constant;
import com.org.manhu.chargehelper.custom.BatteryCharger;
import com.org.manhu.chargehelper.tools.AppManager;
import com.org.manhu.chargehelper.tools.SharedPreferncesUtil;
import com.org.manhu.chargehelper.ui.fragment.AwardLogFragment;
import com.org.manhu.chargehelper.ui.fragment.CostLogFragment;

/**
 * @文件名：CenterActivity.java
 * @包名：com.org.manhu.chargehelper.ui
 * @作者：李朋辉 @QQ：892839211
 * @创建时间：2015年2月5日 下午3:22:15
 *
 * @描述：用户中心
 */
public class CenterActivity extends FragmentActivity implements OnClickListener{

	private ImageView backIv;
	private RadioGroup tabRg;
	private FragmentManager fm;
	private FragmentTransaction ft;
	
	private AwardLogFragment awardFragment;
	private CostLogFragment costFragment;
	private Button cashOutBtn, exchangeBtn;
	private TextView awardNumTv,userNameTv;
	private BatteryCharger battery;
	private UpdateNickNameReceiver mReceiver;
	private UpdateMoneyReceiver receivers;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_center);
		AppManager.getAppManager().addActivity(this);
		
		backIv = (ImageView) findViewById(R.id.title_back);
		battery = (BatteryCharger) findViewById(R.id.battery_charge);
		tabRg = (RadioGroup) findViewById(R.id.user_center_tab);
		cashOutBtn = (Button) findViewById(R.id.tixian_btn);
		cashOutBtn.setVisibility(View.GONE);
		exchangeBtn = (Button) findViewById(R.id.exchange_btn);
		userNameTv = (TextView) findViewById(R.id.user_name);
		awardNumTv = (TextView) findViewById(R.id.user_award_num);
		awardNumTv.setText(SharedPreferncesUtil.getMoney(this) + "");
		userNameTv.setText(SharedPreferncesUtil.getNickName(this));
		
		if(ChargeApplication.isBoxConnected){
			battery.setVisibility(View.VISIBLE);
			battery.startChargerAnim();
		}
		
		awardFragment = new AwardLogFragment();
		costFragment = new CostLogFragment();
		
		fm = getSupportFragmentManager();
		ft = fm.beginTransaction();
		ft.add(R.id.center_tab_fragment, awardFragment);
		ft.show(awardFragment);
		ft.hide(costFragment);
		ft.commit();
		
		tabRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.user_award_log:
					ft = fm.beginTransaction();
					costFragment.onPause();
					if (awardFragment.isAdded()) {
						awardFragment.onResume();
					} else {
						ft.add(R.id.center_tab_fragment, awardFragment);
					}
					ft.hide(costFragment);
					ft.show(awardFragment);
					ft.commit();
					break;
				case R.id.user_tixian_exchange_log:
					ft = fm.beginTransaction();
					awardFragment.onPause();
					if (costFragment.isAdded()) {
						costFragment.onResume();
					} else {
						ft.add(R.id.center_tab_fragment, costFragment);
					}
					ft.hide(awardFragment);
					ft.show(costFragment);
					ft.commit();
					break;
				}
			}
		});
		backIv.setOnClickListener(this);
		cashOutBtn.setOnClickListener(this);
		exchangeBtn.setOnClickListener(this);
		userNameTv.setOnClickListener(this);
		
		mReceiver = new UpdateNickNameReceiver(userNameTv);
		IntentFilter filter = new IntentFilter();
		filter.addAction(Constant.ACTION_UPDATE_NICKNAME);
		registerReceiver(mReceiver, filter);
		
		receivers = new UpdateMoneyReceiver(awardNumTv);
		IntentFilter filters = new IntentFilter();
		filter.addAction(Constant.ACTION_UPDATE_MONEY);
		registerReceiver(receivers, filters);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		awardNumTv.setText(SharedPreferncesUtil.getMoney(this)+"");
		userNameTv.setText(SharedPreferncesUtil.getNickName(this));
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(mReceiver);
		unregisterReceiver(receivers);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			this.finish();
			break;
		case R.id.tixian_btn:
			Intent it = new Intent(this, CashOutActivity.class);
			startActivity(it);
			break;
		case R.id.exchange_btn:
			Intent its = new Intent(this, ScanActivity.class);
			startActivity(its);
			break;
		case R.id.user_name:
			Intent ite = new Intent(this, ModifyNickNameActivity.class);
			ite.putExtra("nickname", userNameTv.getText().toString());
			startActivity(ite);
			break;
		}
	}

}
